/* import home from '@/store/home' */
import Vue from 'vue'
import VueRouter from 'vue-router'
/* import Home from '../views/Home/index.vue'
import Login from '../views/Login/index.vue'
import Register from '../views/Register/index.vue'
import Search from '../views/Search/index.vue'
import Detail from '../views/Detail/index.vue'
import AddShopcart from '../views/AddCartSuccess/index.vue'
import Shopcart from "../views/AddCartSuccess/ShopCart.vue"
import Pay from '../views/Pay/index.vue'
import trade from '../views/Trade'
import PaySuccess from '../views/PaySuccess'
import Center from '../views/Center/index.vue' */
Vue.use(VueRouter)
import store from '../store/index'
//二级路由组件
/* import MyOrder from '../views/Center/myCenter/index.vue';
import groupOrder from '../views/Center/groupCenter/index.vue'; */
const routes = [
  {
    //个人页面
    path: '/center',
    component: ()=>import('../views/Center/index.vue'),
    name: 'Center',
    meta: {
      show: true
    },
    //二级路由
    children: [
      {
        path: 'myorder',
        component: ()=>import('../views/Center/myCenter/index.vue')
      }, {
        path: 'grouporder',
        component: ()=>import('../views/Center/groupCenter/index.vue')
      }, {
        path:'/center',
        redirect:'/center/myorder'
      }
    ]
  },
  {
    //支付成功页面
    path: '/paysuccess',
    component: ()=>import('../views/PaySuccess'),
    name: 'PaySuccess',
    meta: {
      show: true
    }
  },
 //支付页面
  {
   
    path: '/pay',
    component: ()=>import('../views/Pay/index.vue'),
    name: 'Pay',
    meta: {
      show: true
    },
    beforeEnter: (to, from,next) => {
      if (from.path=='/trade') {
        next()
      }else{
        next(false)
      }
    }
  },
 //结算页面
  {
   
    path: '/trade',
    component: ()=>import('../views/Trade/index.vue'),
    name: 'Trade',
    meta: {
      show: true
    },
    //路由独享守卫
    beforeEnter: (to, from,next) => {
      if (from.path=='/shopcart') {
        next()
      }else{
        next(false)
      }
    }
  },
  //重定向，在项目跑起来 的时候访问/ 时 立马定向到重定向页面
  {
    path: '/',
    redirect: '/home',
    name: 'home'
  },
  {
    path: '/home',
    component: ()=>import('../views/Home/index.vue'),
    name: 'home',
    meta: {
      show: true
    }
  },
  {
    path: '/login',
    component: ()=>import('../views/Login/index.vue'),
    name: 'login',
    meta: {
      show: false
    }
  },
  {
    path: '/register',
    component: ()=>import('../views/Register/index.vue'),
    name: 'register',
    meta: {
      show: false
    }
  },
  {
    path: '/search/:keyword?',
    component: ()=>import('../views/Search/index.vue'),
    name: 'search',
    meta: {
      show: true
    }
  },
  {
    path: '/detail/:skuid',
    component: ()=>import('../views/Detail/index.vue'),
    name: 'Detail',
    meta: {
      show: true
    }
  },
  {
    path: '/addshopcart',
    component: ()=>import('../views/AddCartSuccess/index.vue'),
    name: 'addshopcart',
    meta: {
      show: true
    }
  },
  {
    path: '/shopcart',
    component: ()=>import('../views/AddCartSuccess/ShopCart.vue'),
    name: 'Shopcart',
    meta: {
      show: true
    }
  }
]

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    return { y: 0 }
  }
})
//全局守卫：前置守卫（在路由跳转之间进行判断）
router.beforeEach(async (to, from, next) => {
  //to:获取到要跳转到的路由信息
  //from：获取到从哪个路由跳转过来来的信息
  //next: next() 放行  next(path) 放行
  //方便测试 统一放行
  //  next();
  //获取仓库中的token-----可以确定用户是登录了
  let token  = store.state.user.token;
  let name = store.state.user.userinfo.name;
  //用户登录了
  if(token){
      //已经登录而且还想去登录------不行
      if(to.path=="/login"){
          next('/home');
      }else{
          //已经登陆了,访问的是非登录与注册
          //登录了且拥有用户信息放行
          if(name){
              next();
          }else{
              //登陆了且没有用户信息
              //在路由跳转之前获取用户信息且放行
              try {
                  await store.dispatch('getUserInfo');
                  next();
              } catch (error) {
                  //token失效从新登录
                  await store.dispatch('userLogout');
                  next('/login')
              }
          }
      }
  }else{
      //未登录：不能去交易相关、不能去支付相关【pay|paysuccess】、不能去个人中心
      //未登录去上面这些路由-----登录
      let toPath = to.path;
      if(toPath.indexOf('/trade')!==-1 || toPath.indexOf('/pay')!==-1||toPath.indexOf('/center')!==-1){
          //把未登录的时候向去而没有去成的信息，存储于地址栏中【路由】
          next('/login?redirect='+toPath);
      }else{
          //去的不是上面这些路由（home|search|shopCart）---放行
          next();
      }

  }
});

export default router
